import 'dart:async';
import 'package:eh_mobile/common/style/index.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class TempControlController extends GetxController {
  double brightnessValue = .5;
  String tempViewId = 'tempViewId';
  String tempValueViewId = 'tempValueViewId';
  String addViewId = 'addViewId';
  String choseModeViewId = 'choseModeViewId';
  String currentTempValueViewId = 'currentTempValueViewId';
  String badgeWindValueViewId = 'badgeWindValueViewId'; //角标视图
  String bottomScrollViewId = 'bottomScrollViewId'; //角标视图
  bool isShow = false; //是否显示加号或者减号
  double tempValue = 25; //当前设置温度
  double maxValue = 32; //设置最大目标温度
  double minValue = 16; //设置最小目标温度
  Timer? touchTimer; //触摸定时器
  Timer? addTimer; //点击加号定时器
  Timer? subTimer; //点击减号定时器
  int timeSecond = 10;
  double opacity = 0.0;
  double currentTempValue = 16; //当前温度
  double currentMaxValue = 32; //设置最大目标温度
  double currentMinValue = 16; //设置最小目标温度
  int currentWindValue = 1; //当前风速
  int maxWindValue = 3; //最大风速
  List<String> choseModelList = [
    '制冷',
    '制热',
    '除湿',
    '静音',
    '仅送风',
    '自动',
  ];
  String choseModeItemName = '';
  String choseModeIconName = 'assets/svgs/temp_control/ic_cooling_unsel.svg';
  String choseModeIconColor = '#05B9EB';
  bool isOffSwitch = true; //是否关闭开关

  //根据状态获取图标颜色
  Color getDifferentColor() {
    if (isOffSwitch) {
      return HexColor(choseModeIconColor);
    }
    return BnyColor.gray238IconColor;
  }

  //根据状态获左边第一个图标背景颜色
  Color getDifferentHotBgColor() {
    if (isOffSwitch) {
      return choseModeItemName == '制热'
          ? HexColor('1AFA6D5A')
          : const Color.fromRGBO(5, 185, 235, 0.1);
    }
    return BnyColor.gray238BgColor;
  }

  //根据状态获取图标背景颜色
  Color getDifferentBgColor() {
    if (isOffSwitch) {
      return const Color.fromRGBO(5, 185, 235, 0.1);
    }
    return BnyColor.gray238BgColor;
  }

  //中间风扇图标背景色
  Color getDifferentBgColorFromWindIcon() {
    if (isOffSwitch) {
      return BnyColor.lightAlphaWhiteColor;
    }
    return BnyColor.gray238BgColor;
  }

//中间风扇图标颜色
  Color getDifferentIconColorFromWindIcon() {
    if (isOffSwitch) {
      return BnyColor.lightWhiteColor;
    }
    return BnyColor.gray238IconColor;
  }

  //中间风扇上面的数字颜色
  Color getDifferentIconColorFromNumIcon() {
    if (isOffSwitch) {
      return BnyColor.lightWhiteColor;
    }
    return BnyColor.gray238LightIconColor;
  }

  //隐藏掉加减符合视图
  void hiddenAddView(Timer timer) {
    if (isShow) {
      isShow = false;
      opacity = 0.0;
      update([addViewId]);
      if (timer.isActive) {
        timer.cancel();
      }
    }
  }

  //处理触摸数字逻辑
  void addObserverTimer() {
    cancelAllTimer();
    touchTimer = Timer.periodic(Duration(seconds: timeSecond), (myTimer) {
      hiddenAddView(myTimer);
    });
  }

  //加加逻辑
  void addAction() {
    cancelAllTimer();
    addTimer = Timer.periodic(Duration(seconds: timeSecond), (myTimer) {
      hiddenAddView(myTimer);
    });

    if (opacity == 0 || tempValue == maxValue) {
      return;
    }
    tempValue += 1;
    update([tempValueViewId]);
  }

  //当前温度加加逻辑
  void tempAddChangeAction() {
    cancelAllTimer();
    addTimer = Timer.periodic(Duration(seconds: timeSecond), (myTimer) {
      hiddenAddView(myTimer);
    });
    if (opacity == 0 || currentTempValue == currentMaxValue) {
      return;
    }
    currentTempValue = currentTempValue + 0.5;
    update([currentTempValueViewId]);
  }

  //当前温度减减逻辑
  void tempSubChangeAction() {
    cancelAllTimer();
    subTimer = Timer.periodic(Duration(seconds: timeSecond), (myTimer) {
      hiddenAddView(myTimer);
    });
    if (opacity == 0 || currentTempValue == currentMinValue) {
      return;
    }
    currentTempValue = currentTempValue - 0.5;
    update([currentTempValueViewId]);
  }

//减减逻辑
  void subAction() {
    cancelAllTimer();
    subTimer = Timer.periodic(Duration(seconds: timeSecond), (myTimer) {
      hiddenAddView(myTimer);
    });
    if (opacity == 0 || tempValue == minValue) {
      return;
    }
    tempValue -= 1;
    update([tempValueViewId]);
  }

  //根据选择的模式返回不同的图标
  void getIconName(String name) {
    choseModeItemName = name;
    switch (name) {
      case '制冷':
        {
          choseModeIconName = 'assets/svgs/temp_control/ic_cooling_unsel.svg';
          choseModeIconColor = '05B9EB';
        }
      case '制热':
        {
          choseModeIconName = 'assets/svgs/light_control/ic_brightness.svg';
          choseModeIconColor = 'FA6D5A';
        }
      case '除湿':
        {
          choseModeIconName =
              'assets/svgs/temp_control/ic_dehumidification.svg';
          choseModeIconColor = 'EEEEEE';
        }
      case '静音':
        {
          choseModeIconName = 'assets/svgs/temp_control/ic_silent.svg';
          choseModeIconColor = 'EEEEEE';
        }
      case '仅送风':
        {
          choseModeIconName = 'assets/svgs/temp_control/ic_speed_unsel.svg';
          choseModeIconColor = 'EEEEEE';
        }
      case '自动':
        {
          choseModeIconName = 'assets/svgs/temp_control/ic_auto_unsel.svg';
          choseModeIconColor = 'EEEEEE';
        }
    }
  }

//选择模式切换图标
  void choseDifferentMode(String name) {
    getIconName(name);
    update([choseModeViewId]);
  }

  //销毁正在运行的定时器
  void cancelIsActiveTimer(Timer? activeTimer) {
    if (activeTimer != null && activeTimer.isActive) {
      activeTimer.cancel();
    }
  }

  //销毁所有的定时器
  void cancelAllTimer() {
    cancelIsActiveTimer(touchTimer);
    cancelIsActiveTimer(addTimer);
    cancelIsActiveTimer(subTimer);
  }

  //关闭按钮
  void closeBtnAction() {
    isOffSwitch = !isOffSwitch;
    if (isShow) {
      isShow = false;
      opacity = 0.0;
      cancelAllTimer();
    }
    update([tempViewId]);
  }

  //传入温度进行变化
  Color? getTemperatureColor(double temperature) {
    // Define your temperature-to-color mapping logic here
    double minTemperature = 16;
    double maxTemperature = 32;

    final double normalizedTemperature =
        (temperature - minTemperature) / (maxTemperature - minTemperature);
    return Color.lerp(
        HexColor('#05b9eb'), HexColor('ffc437'), normalizedTemperature);
  }

  @override
  void onClose() {
    cancelAllTimer();
    super.onClose();
  }

  @override
  void dispose() {
    cancelAllTimer();
    super.dispose();
  }
}
